/**
 * Gestion d'un panneau d'attente.
 */
function WaitManager() {
    this.overlayId = "ajax-indicator-overlay";
    this.messageId = "ajax-indicator-message";
    this.message = "Chargement...";
    this.overlayOpacity = 0.5;
    this.overlayDuration = 0.4;

    this.init = function(container) {
        // ajout des éléments html au DOM.
        // <div id="overlay-wait" style="display: none;"></div>
        var overlay = Builder.node("div", {
            id :this.overlayId,
            style :"display: none;"
        });

        // <div id="message-wait" style="display: none;"><span>Chargement...</span></div>
        var message = Builder.node("div", {
            id :this.messageId,
            style :"display: none;"
        }, [
            Builder.node("span", {
                id :"message"
            }, this.message)
        ]);

        $(container).insert( {
            top :message
        });
        $(container).insert( {
            top :overlay
        });
    };

    /**
     * Affiche l'attente.
     */
    this.start = function() {
        // stretch overlay to fill page and fade in
        var arrayPageSize = getPageSize();
        $(this.overlayId).setStyle( {
            width :arrayPageSize[0] + 'px',
            height :arrayPageSize[1] + 'px'
        });

        new Effect.Appear($(this.overlayId), {
            duration :this.overlayDuration,
            from :0.0,
            to :this.overlayOpacity
        });

        // calculate top and left offset for the lightbox
        var arrayPageScroll = document.viewport.getScrollOffsets();
        var waitingTop = arrayPageScroll[1] + (document.viewport.getHeight() / 10);
        var waitingLeft = arrayPageScroll[0];
        // $(this.messageId).setStyle({ top: waitingTop + 'px', left: waitingLeft + 'px' }).show();

        // cache les éléments qui resteraient par dessus l'overlay
        $$('select', 'object', 'embed').each( function(node) {
            node.style.visibility = 'hidden';
            node.setAttribute("waitManagerVisibility", "hidden");
        });

        // affiche l'overlay
        new Effect.Appear($(this.messageId), {
            duration :0.0,
            from :0.0,
            to :1.0
        });
        return true;
    };

    /**
     * Ferme l'attente.
     */
    this.stop = function() {
        $(this.messageId).hide();
        $(this.overlayId).hide();

        // reaffiche les éléments qui ont été caché pour l'overlay
        $$('select', 'object', 'embed').each( function(node) {
            if (node.getAttribute("waitManagerVisibility") == "hidden") {
                node.style.visibility = 'visible';
                node.setAttribute("waitManagerVisibility", "visible");
            }
        });
        return true;
    };
};
